package com.zptc.ygz;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.net.Socket;

public class Client {
    private DefaultListModel dt;
    private JTextArea jta;
    private JTextField jta1 = new JTextField();
    private JTextField jtf_message = new JTextField(30);
    private JTextField jtf_port = new JTextField("9999");
    private JTextField jtf_max = new JTextField("30");
    private JButton jbt_start = new JButton("连接");
    private JButton jbt_stop = new JButton("断开");
    private JButton jbt_seed = new JButton("发送");
    Socket socket;

    public void createUI() {
        JFrame jf = new JFrame("客户端");
        jf.setBounds(200, 200, 500, 300);
        //北边Jpanel
        JPanel northPane = new JPanel();
        northPane.setLayout(new GridLayout(1, 8));
        northPane.add(new JLabel("人数上限"));
        northPane.add(jtf_max);
        northPane.add(new JLabel("端口"));
        northPane.add(jta1);
        northPane.add(new JLabel("昵称"));
        northPane.add(jtf_port);
        northPane.add(jbt_start);
        jbt_stop.setEnabled(false);
        northPane.add(jbt_stop);
        northPane.setBorder(new TitledBorder("aaa"));
        //中间Jpanl(左右分栏)
        //左栏
        dt = new DefaultListModel();
        final JList jl = new JList(dt);
        JScrollPane leftpane = new JScrollPane(jl);
        leftpane.setBorder(new TitledBorder("aaa"));
        //右栏
        jta = new JTextArea();
        jta.setEnabled(false);
        JScrollPane reghtpane = new JScrollPane(jta);
        reghtpane.setBorder(new TitledBorder("消息显示区"));
        JSplitPane centPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftpane, reghtpane);
        //设置分割条大小位置
        centPane.setDividerSize(20);
        centPane.setDividerLocation(100);
        //南部Jpanel
        JPanel southPanel = new JPanel();
        southPanel.setBorder(new TitledBorder("写消息"));
        southPanel.add(jtf_message, BorderLayout.CENTER);
        southPanel.add(jbt_seed);
        jf.setLayout(new BorderLayout());
        jf.add(northPane, BorderLayout.NORTH);
        jf.add(centPane, BorderLayout.CENTER);
        jf.add(southPanel, BorderLayout.SOUTH);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        ClientEvent();
    }

    private void ClientEvent() {
        jtf_message.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar()==KeyEvent.VK_ENTER){
                    try {
                        PrintWriter pw = new PrintWriter(socket.getOutputStream());
                        String name = jta1.getText().trim();
                        String yyy = jtf_message.getText();
                        pw.println(name+"#"+yyy);
                        jta.append("客户端说："+yyy+"\n");
                        jtf_message.setText("");
                        pw.flush();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });



        jbt_start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    socket = new Socket("127.0.0.1",9999);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                new Thread(new Runnable() {
                         @Override
                         public void run() {
                             try {
                                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                                 while (true) {
                                 String str = bufferedReader.readLine();
                                 jta.append("服务器说："+str + "\n");
                                 }
                             } catch (IOException e1) {
                                 e1.printStackTrace();
                             }
                         }
                     }).start();
            }
        });
    }


    public static void main(String[] args) {
        new Client().createUI();
    }
}
